package com.geezdata.cps.sdk3rd.wyeth.response;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.base.utils.DateUtil;
import com.geezdata.cps.sdk3rd.wyeth.domain.WyethCartItem;
import com.geezdata.cps.sdk3rd.wyeth.domain.WyethOrderInfo;

public class WyethQueryOrderResult extends WyethResult {
    private Integer record;
    
    private List<WyethOrderInfo> orderInfoList;
    
    public Integer getRecord() {
        return record;
    }

    public void setRecord(Integer record) {
        this.record = record;
    }

    public List<WyethOrderInfo> getOrderInfoList() {
        return orderInfoList;
    }

    public void setOrderInfoList(List<WyethOrderInfo> orderInfoList) {
        this.orderInfoList = orderInfoList;
    }
    
    //由于抽奖订单不放入orderInfoList,所以record！=orderInfoList.size()
    public void parseResponseJsonStringToResponse(String respJsonStr) throws MyException {
        JSONObject json = JSON.parseObject(respJsonStr);             
        String code = json.getString("code");
       
        if( code.equals("1") ) {
            String record = json.getString("record");
            if( StringUtils.isNumeric(record) ) {
                this.record = Integer.parseInt(record);
            }
            
            if(this.record!=null && this.record >0) {
                JSONArray datas=json.getJSONArray("data");
                this.orderInfoList = new ArrayList<WyethOrderInfo>();
                //保证相同id的order不重复取出
                ArrayList<String> orderIdList = new ArrayList<String>();
                for (int i = 0; i < datas.size(); i++) {
                    JSONObject data = datas.getJSONObject(i);  
                    
                    String id = data.getString("id");
                    if(!orderIdList.contains(id)){
                        
                        WyethOrderInfo orderInfo = new WyethOrderInfo();
                        //是否为抽奖订单
                        boolean isPrize = false;

                        List<WyethCartItem> cartItemList = new ArrayList<WyethCartItem>();
                        JSONArray carts = data.getJSONArray("carts");
                        for(int j = 0; j < carts.size(); j++) {
                            WyethCartItem cartItem = new WyethCartItem();
                            JSONObject cart = carts.getJSONObject(j);
                            
                            //抽奖订单，商品ID为空                       
                            if(StringUtils.isEmpty(cart.getString("productNo"))) {
                                isPrize = true;
                                continue;
                            }   
                              
                            cartItem.setIsRefund(new Integer(cart.getString("isRefund")));
                            cartItem.setProductName(cart.getString("productName"));
                            cartItem.setProductNo(cart.getString("productNo"));
                            cartItem.setProductPrice(new BigDecimal(cart.getString("productPrice")));
                            cartItem.setProductQuantity(new Integer(cart.getString("productQuantity")));
                            
                            cartItemList.add(cartItem);
                        }
                        
                        if(isPrize && cartItemList.size()==0){
                            //订单为抽奖订单
                            continue;
                        }
                        
                        orderInfo.setCarts(cartItemList);
                        
                        orderInfo.setAddress(data.getString("address"));
                        orderInfo.setAmount(new BigDecimal(data.getString("amount")));
                        orderInfo.setArea(data.getString("area"));
                        orderInfo.setCity(data.getString("city"));
                        orderInfo.setComment(data.getString("comment"));
                        orderInfo.setContact(data.getString("contact"));
                        orderInfo.setDeclareStatus(new Integer(data.getString("declareStatus")));
                        orderInfo.setId(id);
                        orderInfo.setIdCard(data.getString("idCard"));
                        orderInfo.setIsCrossBorder(new Integer(data.getString("isCrossBorder")));
                        orderInfo.setName(data.getString("name"));
                        orderInfo.setOutNo(data.getString("outNo"));
                        orderInfo.setPayTime(DateUtil.parseDateRuntime(data.getString("payTime")));
                        orderInfo.setProvince(data.getString("province"));
                        orderInfo.setShippingFee(new BigDecimal(data.getString("shippingFee")));
                        orderInfo.setStatusText(data.getString("statusText"));
                        
                        orderIdList.add(id);
                        this.orderInfoList.add(orderInfo);
                    }
                }
            }
        } else {
        	throw new MyException(ErrorCode.INTERNAL_SERVER_ERROR.getCode(),  
        			json.getString("code") + json.getString("message"));
        }
    } 
}
